package clean_handler

import (
	"file-cleaner/internal/logger"
	"file-cleaner/internal/pkg/file_cleaner/cleaner"
	"file-cleaner/internal/pkg/file_cleaner/file_info"
	"os"
	"strings"
)

type CleanHandler interface {
	doHandle(info *cleaner.CleanTaskInfo) bool
}

type DeleteFileImpl struct {
}

func (d DeleteFileImpl) doHandle(info *cleaner.CleanTaskInfo) bool {
	//if info.TargetFileInfo().Hash() == "" {
	//	return false
	//}

	if info.SourceFileInfo().GetTag() != file_info.OnlyDelete && strings.EqualFold(info.SourceFileInfo().Path(), info.TargetFileInfo().Path()) {
		logger.Infof("Skip delete target file: %v", info.TargetFileInfo().Path())
		return true
	}

	err := os.Remove(info.SourceFileInfo().Path())
	if err != nil {
		logger.Error(err)
		return false
	}
	logger.Infof("Finished deleting file: %v", info.SourceFileInfo().Path())
	return true
}

type LinkFileImpl struct {
}

func (l LinkFileImpl) doHandle(info *cleaner.CleanTaskInfo) bool {

	if info.SourceFileInfo().GetTag() != file_info.OnlyDelete && strings.EqualFold(info.SourceFileInfo().Path(), info.TargetFileInfo().Path()) {
		logger.Infof("Skip hard link target file: %v", info.TargetFileInfo().Path())
		return true
	}

	err := os.Link(info.TargetFileInfo().Path(), info.SourceFileInfo().Path())
	if err != nil {
		logger.Error(err)
		return false
	}
	logger.Infof("Finished hard link file: %v (size: %v, hash: %v), to file: %v (size: %v, hash: %v)", info.SourceFileInfo().Path(), info.SourceFileInfo().Size(), info.SourceFileInfo().Hash(), info.TargetFileInfo().Path(), info.TargetFileInfo().Size(), info.TargetFileInfo().Hash())
	return true
}

type DryRunImpl struct {
}

func (d DryRunImpl) doHandle(info *cleaner.CleanTaskInfo) bool {
	logger.Infof("DryRun File name: %v", info.SourceFileInfo().Path())

	return true
}
